package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

type RealServe struct {
	Addr string
}

func (s *RealServe) Run() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", s.HelloHandler)
	server := &http.Server{
		Addr:         s.Addr,
		WriteTimeout: time.Second * 3,
		Handler:      mux,
	}

	go func() {
		log.Fatal(server.ListenAndServe())
	}()
}

func (s *RealServe) HelloHandler(w http.ResponseWriter, r *http.Request) {

	io.WriteString(w, fmt.Sprintf("addr:%s , path:%s \n", s.Addr, r.URL.Path))
	io.WriteString(w, fmt.Sprintf("RemoteAddr:%s , X-Forwarded-For:%s , X-Real-Ip:%s\n", r.RemoteAddr, r.Header.Get("X-Forwarded-For"), r.Header.Get("X-Real-Ip")))
	io.WriteString(w, fmt.Sprintf("%v\n", r.Header))

}

func main() {
	s1 := &RealServe{
		Addr: "127.0.0.1:8000",
	}
	s1.Run()

	s2 := &RealServe{
		Addr: "127.0.0.1:8001",
	}
	s2.Run()

	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
}
